package com.yishe.biz.login;

import com.alibaba.fastjson.JSON;
import com.yishe.api.RestCodeConstants;
import com.yishe.biz.mysql.YsStaffBiz;
import com.yishe.config.ObjectRestResponse;
import com.yishe.entites.vo.UserVo;
import com.yishe.entites.mysql.WxUser;
import com.yishe.entites.mysql.YsStaff;
import com.yishe.mapper.mysql.WxUserMapper;
import com.yishe.redis.RedisUtils;
import com.yishe.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : chusifeng
 * @version :
 * @date :Create in 2021/4/14 11:02
 * @description: 登录
 * @package com.yishe.biz.login
 * @className LoginBiz
 */
@Service
@Slf4j
public class LoginBiz {

    @Value("${yishe.user.token.expireTime:1800}")
    private int tokenExpireTime;
    @Resource
    private YsStaffBiz ysStaffBiz;
    @Resource
    private WxUserMapper wxUserMapper;
    /**
     * create by: chusifeng
     * Create in 2021/4/9 10:00
     * Description:员工登录
     * <p>Example</p>
     */
    public ObjectRestResponse yslogin(Map<String, String> param) {
        ObjectRestResponse restResponse = new ObjectRestResponse();
        //根据姓名，手机号，权限查询用户
        YsStaff ysStaff = ysStaffBiz.getUserInfoByParam(param);
        // 登录成功,获取登录凭证token/用户信息及其权限菜单信息
        if (null != ysStaff) {
            /**
             * 2. 登录日志信息存储
             */
            // 添加登录日志?
            //生成token
            String token = CommonUtils.getToken();
            /**
             * 3. 登录用户信息缓存存储
             */
            String loginJson = JSON.toJSONString(ysStaff);
            RedisUtils instance = RedisUtils.getInstance();
            // 用户相关信息入缓存
            instance.set(RestCodeConstants.CommonConstants.REGION_LGOIN, token, loginJson, tokenExpireTime);

            UserVo userVo = new UserVo();
            userVo.setToken(token);
            userVo.setUserInfo(ysStaff);
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("userInfo", userVo);
            restResponse.setResultMsg("登录成功!");
            restResponse.setData(data);
        } else {
            restResponse.setErrorMsg("登陆失败！请检查输入信息！");
        }
        return restResponse;
    }

    public ObjectRestResponse wxlogin(Map<String, String> param) {
        ObjectRestResponse restResponse = new ObjectRestResponse();
        //根据姓名，手机号，权限查询用户
        WxUser wxUser = new WxUser();
        wxUser.setTelePhone(param.get("telt_phone"));
        wxUser.setPassword(param.get("password"));
        wxUser = wxUserMapper.selectOne(wxUser);
        // 登录成功,获取登录凭证token/用户信息及其权限菜单信息
        if (null != wxUser) {
            /**
             * 2. 登录日志信息存储
             */
            // 添加登录日志?
            //生成token

            String token = CommonUtils.getToken();
            /**
             * 3. 登录用户信息缓存存储
             */
            String loginJson = JSON.toJSONString(wxUser);
            RedisUtils instance = RedisUtils.getInstance();
            // 用户相关信息入缓存
            instance.set(RestCodeConstants.CommonConstants.REGION_LGOIN, token, loginJson, tokenExpireTime);

            UserVo userVo = new UserVo();
            userVo.setToken(token);
            userVo.setWxUser(wxUser);
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("userInfo", userVo);
            restResponse.setResultMsg("登录成功!");
            restResponse.setData(data);
        } else {
            restResponse.setErrorMsg("登陆失败！请检查输入信息！");
        }
        return restResponse;
    }
}
